package com.zero.qth.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zero.common.core.domain.page.PageDomain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zero.common.annotation.log.Log;
import com.zero.common.core.basecontroller.BaseController;
import com.zero.common.core.domain.result.AjaxResult;
import com.zero.common.core.enums.BusinessType;
import com.zero.qth.domain.Users;
import com.zero.qth.service.IUsersService;
import com.zero.common.core.utils.poi.ExcelUtil;
import com.zero.common.core.domain.page.TableDataInfo;

/**
 * 用户列表Controller
 *
 * @author cai-xu-kun
 * @date 2025-08-02
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "用户列表")
@RequestMapping("/qth/users")
public class UsersController extends BaseController {

    private final IUsersService usersService;

    /**
     * 查询用户列表分页
     */
    @ApiOperation("查询用户列表分页")
    @PreAuthorize("@ss.hasPermi('qth:users:list')")
    @GetMapping("/list")
    public TableDataInfo list(Users users) {
        PageDomain pageDomain = this.startPage();
        Page<Users> p = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        Page<Users> page = usersService.selectUsersListPage(users, p);
        return this.getDataTable(page);
    }

    /**
     * 导出用户列表列表
     */
    @ApiOperation("导出用户列表列表")
    @PreAuthorize("@ss.hasPermi('qth:users:export')")
    @Log(title = "用户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Users users) {
        List<Users> list = usersService.selectUsersList(users);
        ExcelUtil<Users> util = new ExcelUtil<Users>(Users.class);
        util.exportExcel(response, list, "用户列表数据");
    }

    /**
     * 获取用户列表详细信息
     */
    @ApiOperation("获取用户列表详细信息")
    @PreAuthorize("@ss.hasPermi('qth:users:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(usersService.selectUsersById(id));
    }

    /**
     * 新增用户列表
     */
    @ApiOperation("新增用户列表")
    @PreAuthorize("@ss.hasPermi('qth:users:add')")
    @Log(title = "用户列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Users users) {
        return toAjax(usersService.insertUsers(users));
    }

    /**
     * 修改用户列表
     */
    @ApiOperation("修改用户列表")
    @PreAuthorize("@ss.hasPermi('qth:users:edit')")
    @Log(title = "用户列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Users users) {
        return toAjax(usersService.updateUsers(users));
    }

    /**
     * 删除用户列表
     */
    @ApiOperation("删除用户列表")
    @PreAuthorize("@ss.hasPermi('qth:users:remove')")
    @Log(title = "用户列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(usersService.deleteUsersByIds(ids));
    }
}
